package com.yanchangapp.adapter.part;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.model.bean.RecordProductBean;
import com.yanchangapp.widget.ExpandLayout;
import com.zxl.baselib.util.formatter.BigDecimalUtils;
import com.zxl.baselib.util.ui.ResHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 2019-08-28.
 * description:修改数量的adapter
 */
public class ProductEditNumAdapter extends RecyclerView.Adapter<ProductEditNumAdapter.ViewHolder> {
    private Context mContext;
    private List<RecordProductBean> mProductBeans = new ArrayList<>();
    private RecordProductListener mRecordProductListener;
    private boolean isShow = true;
    private boolean isEdit =false;
    public static final int JUST_REFRESH_NUM_SUPPLIER = 0;
    // 设置编辑 + 刷新
    public void setEditType(){
        isEdit = true;
        notifyDataSetChanged();
    }

    public void justSetType(){
        isEdit = true;
    }

    public void setNumShow(boolean isShow){
        this.isShow = isShow;
    }

    public ProductEditNumAdapter(Context context,
                                 List<RecordProductBean> productBeans,
                                 RecordProductListener recordProductListener) {
        mContext = context;
        mProductBeans = productBeans;
        mRecordProductListener = recordProductListener;
    }

    @Override
    public ProductEditNumAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_product_reject_record_approve, parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ProductEditNumAdapter.ViewHolder holder, int position) {
        RecordProductBean productBean = mProductBeans.get(position);
        switch (productBean.getPurchaseStatus()){
            case 2:
                holder.mCardView.setCardBackgroundColor(ResHelper.getColor(R.color.must_bg));
                break;
            case 1:
                holder.mCardView.setCardBackgroundColor(ResHelper.getColor(R.color.sug_bg));
                break;
            default:
                holder.mCardView.setCardBackgroundColor(ResHelper.getColor(R.color.normal_bg));
                break;
        }
        // 设置单价
        holder.tvSinglePrice.setText(BigDecimalUtils.keep2Decimal(productBean.getPrice()));
        if (isShow){
            holder.llSinglePrice.setVisibility(View.VISIBLE);
        }else {
            holder.llSinglePrice.setVisibility(View.GONE);
        }
        holder.tvProductFormat.setText(productBean.getProductFormat());
        holder.tvManufacturer.setText(productBean.getManufacture());
        holder.tvTitle.setText(productBean.getProductName());
        holder.tvSupplierName.setText(productBean.getSupplierName());
        holder.tvBuyNum.setText(ResHelper.getString(R.string.product_buy_num,productBean.getPurchaseNum()));
        holder.tvRealStore.setText(ResHelper.getString(R.string.real_store,
                productBean.getInventoryNumber(),productBean.getAlarmMin(),productBean.getAlarmMax()));
        // 设置图片
        holder.ivArrow.setImageResource(R.mipmap.ic_order_arrow_down);
        holder.expandLayout.initExpand(false,holder.ivArrow,false);
        holder.rlHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.expandLayout.toggleExpand();
            }
        });
        if (isEdit){
            holder.ivEditor.setVisibility(View.VISIBLE);
        }else {
            holder.ivEditor.setVisibility(View.GONE);
        }
        holder.llBuyerNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit && null != mRecordProductListener){
                    mRecordProductListener.onProductNumClick(position);
                }
            }
        });
    }

    // 局部设置number...
    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        RecordProductBean productBean = mProductBeans.get(position);
        if (payloads.isEmpty()){
            onBindViewHolder(holder,position);
        }else {
            int switchIndex = (int)payloads.get(0);
            if (switchIndex == JUST_REFRESH_NUM_SUPPLIER){
                // 仅仅刷新数据即可...
                // 刷新供货商...
                holder.tvBuyNum.setText(ResHelper.getString(R.string.product_buy_num,
                        productBean.getPurchaseNum()));
                // 设置供应商名称...
                holder.tvSupplierName.setText(productBean.getSupplierName());
            }
        }
    }

    @Override
    public int getItemCount() {
        return mProductBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private CardView mCardView;
        private RelativeLayout rlHead;
        private TextView tvTitle,tvBuyNum,tvRealStore,tvSinglePrice,tvProductFormat,tvManufacturer,tvSupplierName;
        private ExpandLayout expandLayout;
        private ImageView ivEditor,ivArrow;
        private LinearLayout llBuyerNum,llSinglePrice;

        public ViewHolder(View itemView) {
            super(itemView);
            mCardView = itemView.findViewById(R.id.cardView);
            rlHead = itemView.findViewById(R.id.rlHead);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvBuyNum = itemView.findViewById(R.id.tvBuyNum);
            tvRealStore = itemView.findViewById(R.id.tvRealStore);
            tvSinglePrice = itemView.findViewById(R.id.tvSinglePrice);
            tvProductFormat = itemView.findViewById(R.id.tvProductFormat);
            tvManufacturer = itemView.findViewById(R.id.tvManufacturer);
            tvSupplierName = itemView.findViewById(R.id.tvSupplierName);
            expandLayout = itemView.findViewById(R.id.expandLayout);
            ivEditor = itemView.findViewById(R.id.ivEdit);
            llBuyerNum = itemView.findViewById(R.id.llBuyerNum);
            ivArrow = itemView.findViewById(R.id.ivArrow);
            llSinglePrice = itemView.findViewById(R.id.singlePrice);
        }
    }

    public interface RecordProductListener{
        void onProductNumClick(int position);
    }
}
